import java.util.Random;
import java.util.Scanner;

/**
 * 游戏类
 * */
public class Game {
    private Player player;

    public Game() {

    }

    public Game(Player player) {
        this.player = player;
    }

    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

    //输出字符串，返回字符串用于和玩家输入比较
    public String printStr() {
        StringBuffer stringBuffer = new StringBuffer();
        int levelNo = player.getLevelNo();
        int strLen = LevelParam.levels[levelNo - 1].getStrLength();
        Random random = new Random();
        for (int i = 0; i < strLen; i++) {
            int rand = random.nextInt(strLen);
            switch (rand) {
                case 0:
                    stringBuffer.append(">");
                    break;
                case 1:
                    stringBuffer.append("<");
                    break;
                case 2:
                    stringBuffer.append("*");
                    break;
                case 3:
                    stringBuffer.append("1");
                    break;
                case 4:
                    stringBuffer.append("2");
                    break;
                case 5:
                    stringBuffer.append("3");
                    break;
            }
        }
        return stringBuffer.toString();

    }

    //比较游戏输出out和玩家输入in，根据比较结果输出的相应信息
    public void printResult(String out, String in) {
        if (out.equals(in)) {
            long currentTime = System.currentTimeMillis();
            //获取到开始游戏的时间差 <=表示没有超时
            if ((currentTime - player.getStartTime()) / 1000 <= LevelParam.levels[player.getLevelNo() - 1].getTimeLimit()){
                int newScore=player.getCurrScore()+LevelParam.levels[player.getLevelNo() - 1].getPerScore();//计算新分数
                player.setCurrScore(newScore);//更新玩家当前分数
                long newTime= ((currentTime - player.getStartTime()) / 1000);
                player.setElapsedTime((int) newTime);//更新玩家当前时间记录
                System.out.println("输入正确，你的等级为:"+player.getLevelNo()+
                        "你的积分为："+player.getCurrScore()+",你的用时为:"
                +player.getElapsedTime()+"秒");
                if (player.getLevelNo()==6){
                    for (int i = 0; i <LevelParam.levels.length ; i++) {
                             //int currentScore;
                    }
                    if (player.getCurrScore()==0){
                        System.out.println("恭喜你全部通关");
                        System.exit(1);
                    }
                }
            }else if ((currentTime - player.getStartTime()) / 1000 > LevelParam.levels[player.getLevelNo() - 1].getTimeLimit()) {
                System.out.println("你输入的太慢了，已经超时，退出了！");
                System.exit(1);
            }
            //player.setCurrScore(player.getCurrScore() + LevelParam.levels[player.getLevelNo() - 1].getPerScore());
        }else {
            System.out.println("你的输入错误，游戏结束，退出游戏");
            System.exit(1);
        }

    }
}
